﻿using FastReport.Data;
using iTextSharp.tool.xml.css;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Office.Interop.Excel;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Data;

namespace LanDBom.Models
{
    public class Bought
    {
        LDbaseEntities db = new LDbaseEntities();
        //计算外购见得阶梯价格
        /*计算过程
         
         1.根据单号寻找第一层的物料的阶梯并拆解
        2.循环遍历*/
        public string Levesjin(string bian = "")
        {
            var App = db.Applications.Where(s => s.AP_bian == bian).FirstOrDefault();
            var Bom = db.BOMini.Where(s => s.BOM2_bian == bian).OrderBy(j => j.BOM2_BQing).ToList();
            var Leve0 = Bom.Where(s => s.BOM2_chen == "Leve0").ToList();
            var sjnf = 0;
            foreach (var item in Leve0)
            {
                //1.根据单号寻找第一层的物料的阶梯并拆解
                var fior = item.BOM2_liao;//料号
                var djbm = item.BOM2_bian;//单据编码
                var bq = item.BOM2_BQing;//标签
                var bb = item.BOM2_Zhuang;//版本
                var shum = db.Formation.Where(s => s.FM_bai == fior).FirstOrDefault();//物料的行
                var Jieti = shum.FM_Jieti.Split(',');
                var bom = db.BOMini.Where(s => s.BOM2_bian == djbm && s.BOM2_BQing.Contains(bq) && s.BOM2_Zhuang == bb && s.BOM2_chen == "leve1").ToList();//获取leve1层物料
                var shund = "";

                //判断有无leve1成   无1层直接算0层
                if (bom.Count() == 0)
                {
                    var Gonged = item.BOM2_Gong;
                    var Operns = db.operational.Where(s => s.OP_ID == Gonged).FirstOrDefault();
                    var jie = Operns == null ? "" : Operns.OP_jiehe == null ? "" : Operns.OP_jiehe;
                    var shun = jie.Split(';');
                    if (item.BOM2_Lei == "自制")
                    {
                        var JTYH = "";
                        for (int i = 0; i < Jieti.Length; i++)
                        {
                            var ji = Jieti[i].ToString();//阶梯
                            var JK = Operns == null ? "0.0" : Operns.OP_price.ToString();
                            JTYH += "" + ":" + Jieti[i] + ":" + JK + ",";
                        }
                        shund += JTYH;
                    }
                    else
                    {
                        var CBS = "";
                        for (int i = 0; i < Jieti.Length; i++)
                        {
                            var ji = Jieti[i].ToString();//阶梯
                            var bid = item.BOM2_id;
                            var infh = db.Informa.Where(s => s.BOM2_id == bid && s.Inf_zhuang == "已确认" && s.Inf_JT == ji && s.Inf_shen == bian).FirstOrDefault();//对应阶梯回价
                            var HuiP = infh == null ?Operns.OP_price.ToString() : infh.Inf_huiP;
                            var INFid = infh == null ? 0 : infh.Inf_id;
                            CBS += INFid + ":" + Jieti[i] + ":" + HuiP + ",";
                        }
                        shund = CBS;
                    }
                }
                else
                {
                    //有1层判断是否存在2层
                    for (int i = 0; i < Jieti.Length; i++)
                    {
                        var jidn = 0.0;
                        var j = "";

                        foreach (var B in bom)
                        {
                            //判断是否存在2层
                            var fior1 = B.BOM2_liao;//料号
                            var djbm1 = B.BOM2_bian;//单据编码
                            var bq1 = B.BOM2_BQing;//标签
                            var bb1 = B.BOM2_Zhuang;//版本
                            var Gong12 = B.BOM2_Gong;//版本
                            var C1 = B.BOM2_Yong;
                            var bom1 = db.BOMini.Where(s => s.BOM2_bian == djbm1 && s.BOM2_BQing.Contains(bq1) && s.BOM2_Zhuang == bb1 && s.BOM2_chen == "leve2").ToList();
                            var gongji = db.operational.Where(s => s.OP_ID == Gong12).FirstOrDefault();
                            var LH = 0;
                            var he = 0.0;
                            he = Leve2(bian, fior, Jieti, B, bom1, he);
                            var lei = B.BOM2_Lei;
                            if (lei == "自制")
                            {
                                he = gongji == null ? 0.0 : double.Parse(gongji.OP_price.ToString());
                                var jige = he * double.Parse(C1);
                                jidn += jige;
                            }
                            else
                            {
                                var JIKML = Jieti[i].ToString();
                                var SHIji = int.Parse(C1) * int.Parse(JIKML);//根据0层阶梯计算1层阶梯所需要的阶梯数量
                                //在询价表里面查找符合条件的物料的价格
                                var inf = db.Informa.Where(s => s.BOM2_id == B.BOM2_id && s.Inf_JT == JIKML && s.Inf_zhuang == "已确认").FirstOrDefault();//获取符合条件的价格
                                he = inf == null ? 0.0 : double.Parse(inf.Inf_huiP);//回价
                                j = inf == null ? "0" : inf.Inf_id.ToString();
                                var husnf = double.Parse(C1) * he;//一个物料的值
                                jidn += husnf;
                            }
                        }
                        shund += j + ":" + Jieti[i] + ":" + jidn + ",";//不同阶梯的不用价格  格式 inf的编号：阶梯：价格
                    }
                }
                //保存到固定的单元格
                item.BOM2_CB = shund;
                db.Entry(item).State = System.Data.Entity.EntityState.Modified;
                var sedr = db.SaveChanges();//将价格保存
                sjnf += sedr;
            }
            var message = sjnf == Leve0.Count ? "Y" : "N";
            return message;
        }

        public void WLGX(string bians)
        {
            //将通过审核的物料保存到Ndardo表
            var bianAPP = db.BOMini.Where(s=>s.BOM2_bian==bians).ToList();
            foreach (var b in bianAPP)
            {
                //保存字段   物料编码 物料成本 
                var A = b.BOM2_liao;
                var Bbian = db.Ndardo.Where(s => s.Fo_id == A).FirstOrDefault();
                
                if (Bbian!=null)
                {
                    Bbian.Nd_danwei = b.BOM2_CB;
                    db.Entry(Bbian).State = System.Data.Entity.EntityState.Modified;
                }
                else
                {
                    var BBI = new Ndardo()
                    {
                        Fo_id = b.BOM2_liao,
                        Nd_danwei = b.BOM2_CB,
                    };
                    db.Ndardo.Add(BBI);
                }
                db.SaveChanges();
            }
        }

        private double Leve2(string bian, string fior, string[] Jieti, BOMini B, List<BOMini> bom1, double he)
        {
            foreach (var C in bom1)
            {
                var ji1 = C.BOM2_liao;//料号
                var Gonged1 = C.BOM2_Gong;
                var BJid = C.BOM2_id;
                var yong = C.BOM2_Yong;//用例
                var shum1 = db.Formation.Where(s => s.FM_bai == fior).FirstOrDefault();//物料的行
                var Operns = db.operational.Where(s => s.OP_ID == Gonged1).FirstOrDefault();
                if (B.BOM2_Lei == "自制")
                {
                    he += double.Parse(Operns.OP_price.ToString());
                }
                else
                {
                    if (Operns.OP_jiehe == null)
                    {
                        he += double.Parse(Operns.OP_price.ToString());
                    }
                    else
                    {
                        var K1 = "";
                        for (int q = 0; q < Jieti.Length; q++)
                        {
                            var OP = Jieti[q] == null ? "3000" : Jieti[q];
                            var shune = db.Informa.Where(s => s.BOM2_id == BJid && s.Inf_zhuang == "已确认" && s.Inf_shen == bian && s.Inf_JT == OP).FirstOrDefault();
                            K1 = shune == null ? Operns.OP_price.ToString() : shune.Inf_huiP;
                        }
                        he += double.Parse(K1);
                    }
                }
            }

            return he;
        }


    }
}